<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>登录界面</title>
  <link rel="stylesheet" href="./css/style.css">
  <link rel="stylesheet" href="./css/body.css">
  <script src="./jquery.js"></script>
  <script src="./axios.js"></script>
</head>

<body>
  <div class="container">
    <section id="content">
      <form>
        <h1>账号登录</h1>
        </h1>
        <div>
          <i class="close">X</i>
          <input type="text" placeholder="手机号" id="username">
        </div>
        <div>
          <i class="close">X</i>
          <input type="password" placeholder="密码" id="password">
        </div>
        <div>
          <input type="submit" value="登录" class="btn btn-primary" id="js-btn-login">
          <a href="">忘记密码?</a>
          <a href="#">注册账号</a>
        </div>
      </form>
    </section>
  </div>
</body>
<script>
  /*接口地址： http://124.223.14.236:3001/api */


  //设置默认请求根路径
  axios.defaults.baseURL = 'http://124.223.14.236:3001/api'

  //获取按钮
  const btn = document.querySelector('#js-btn-login')
  //给按钮注册点击事件,并使用回调地域终极解决方案,async+await解决回调地域问题
  btn.addEventListener('click', async function (e) {

    //阻止表单默认事件
    e.preventDefault()

    //获取用户输入账号
    let username = document.querySelector('#username').value
    //获取用户输入密码
    let password = document.querySelector('#password').value
    //发起axios请求
    const res = await axios.post('/user/login', {
      username,
      password
    })
    console.log("res", res)
    //将token储存到本地
    localStorage.setItem('token', `${res.data.data.token}`)
    //判断是否登录成功
    if (res.data.success !== "true") return alert(res.data.msg)
    alert('登录成功')
  })
</script>

</html>